﻿using com.IronOne.BoardPACWinAppUtil.Enums;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;

namespace com.IronOne.BoardPACWinAppUtil.Util
{
    public static class Utility
    {
        static CultureInfo ci_enUS = new CultureInfo("en-US");


        /// <summary>
        /// Return Enum value as an object / Can use in switch case statements
        /// </summary>
        /// <param name="T"> Enum as a Type</param>
        /// <param name="selectedEnumValue"> Enum value</param>
        /// <returns></returns>
        public static object GetEnumValue(Type T, string selectedEnumValue) 
        {
            try
            {
                object enumValue = (int)Enum.Parse(T, selectedEnumValue);
                return enumValue;
            }
            catch (Exception)
            {                
                throw;
            }
        }
        
        /// <summary>
        /// pass date as string an get en-US formatted Date
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime GetDate(string date) 
        {
            try
            {
                DateTime formattedDate = DateTime.Parse(date, ci_enUS);
                return formattedDate;
            }
            catch (Exception)
            {                
                throw;
            }
        }

        /// <summary>
        /// pass date as DateTime an get en-US formatted Date
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime GetDate(DateTime date)
        {
            try
            {
                string stringDate = date.ToString();
                DateTime formattedDate = DateTime.Parse(stringDate, ci_enUS);
                return formattedDate;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// Pass date as DateTime an get en-US formatted string type Date
        /// NOTE: Not formatted in BoardPAC datetime format
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetDateAsString(DateTime date)
        {
            try
            {
                string formattedDate = date.ToString(ci_enUS);
                return DateTime.Parse(formattedDate).ToString(Global.DateFormat);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// return paper decision status given by board member
        /// </summary>
        /// <param name="dStatus"></param>
        /// <returns></returns>
        public static int GetPaperDecisionStatus(string dStatus) 
        {
            try
            {
                if (dStatus.Equals(DecisionStatus.Abstain.ToString()))
                {
                    return (int)DecisionStatus.Abstain;
                }
                else if (dStatus.Equals(DecisionStatus.Approve.ToString()))
                {
                    return (int)DecisionStatus.Approve;
                }
                else if (dStatus.Equals(DecisionStatus.Interest.ToString()))
                {
                    return (int)DecisionStatus.Interest;
                }
                else if (dStatus.Equals(DecisionStatus.Pending.ToString()))
                {
                    return (int)DecisionStatus.Pending;
                }
                else if (dStatus.Equals(DecisionStatus.Reject.ToString()))
                {
                    return (int)DecisionStatus.Reject;
                }
                else
                {
                    return (int)DecisionStatus.Rpt;
                }
            }
            catch (Exception)
            {                
                throw;
            }
        }

        /// <summary>
        /// return paper status given by board secreratary
        /// </summary>
        /// <param name="paperStatus"></param>
        /// <returns></returns>
        public static int GetPaperStatus(string paperStatus) 
        {
            try
            {
                if (paperStatus.Equals(PaperStatus.Approved.ToString()))
                {
                    return (int)PaperStatus.Approved;
                }
                else if (paperStatus.Equals(PaperStatus.Rejected.ToString()))
                {
                    return (int)PaperStatus.Rejected;
                }
                else
                {
                    return (int)PaperStatus.CarriedForward;
                }
            }
            catch (Exception)
            {                
                throw;
            }
        }

        /// <summary>
        /// Provide twenty four hour time as string and get back twelve hour time in string
        /// </summary>
        /// <param name="twentyfourHourTime">Provide as a string</param>
        /// <returns></returns>
        public static string ConvertTime(string twentyfourHourTime)
        {
            try
            {
                DateTime date = DateTime.Parse(twentyfourHourTime, ci_enUS);
                twentyfourHourTime = date.ToString("h:mm tt", ci_enUS);
            }
            catch (Exception)
            {
            }
            return twentyfourHourTime;
        }

        public static int GetFlyoutDefaultWidth()
        {
            return 350;
        }

        public static int GetMaxCountDisplay()
        {
            return 99;
        }

        public static string GetMaxCountDisplayOnUi()
        {
            return "99+";
        }

        public static int GetAppbarCloseTime()
        {
            return 100;
        }

        public static int GetNoOfPasswordAttempts()
        {
            return 25;
        }

        public static int GetNarrowWidthSize()
        {
            return 500;
        }

        public async static Task<StorageFolder> GetUserFolder()
        {
            try
            {
                return await StorageFolder.GetFolderFromPathAsync(Path.Combine(ApplicationData.Current.LocalFolder.Path, Global.UserId.ToString()));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async static Task<StorageFolder> GetUserPdfFolder()
        {
            try
            {
                return await StorageFolder.GetFolderFromPathAsync(Path.Combine(ApplicationData.Current.LocalFolder.Path, Global.UserId.ToString(), ApplicationConstants.PDF));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async static Task<StorageFolder> GetShareOriginalFolder()
        {
            try
            {
                var userFolder = await GetUserFolder();
                var shareFolder = await userFolder.CreateFolderAsync(ApplicationConstants.SHARE, CreationCollisionOption.OpenIfExists);
                return await shareFolder.CreateFolderAsync(ApplicationConstants.ORIGINAL, CreationCollisionOption.OpenIfExists);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async static Task<StorageFolder> GetShareSentFolder()
        {
            try
            {
                var userFolder = await GetUserFolder();
                var shareFolder = await userFolder.CreateFolderAsync(ApplicationConstants.SHARE, CreationCollisionOption.OpenIfExists);
                return await shareFolder.CreateFolderAsync(ApplicationConstants.SENT, CreationCollisionOption.OpenIfExists);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async static Task<StorageFolder> GetShareReceivedFolder()
        {
            try
            {
                var userFolder = await GetUserFolder();
                var shareFolder = await userFolder.CreateFolderAsync(ApplicationConstants.SHARE, CreationCollisionOption.OpenIfExists);
                return await shareFolder.CreateFolderAsync(ApplicationConstants.RECEIVED, CreationCollisionOption.OpenIfExists);
            }
            catch (Exception)
            {
                throw;
            }
        }


        public async static Task<IRandomAccessStream> ToStream(IBuffer ibuffer)
        {
            try
            {
                var stream = new InMemoryRandomAccessStream();
                var outputStream = stream.GetOutputStreamAt(0);
                var datawriter = new DataWriter(outputStream);
                datawriter.WriteBuffer(ibuffer);
                await datawriter.StoreAsync();
                await outputStream.FlushAsync();
                return stream;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string GetDateTimeFormat()
        {
            try
            {
                return Global.DateFormat + " h:mm tt";
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string GetTimeFormat()
        {
            try
            {
                return "h:mm tt";
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
